package com.ocean.courtyard.command;

import com.ocean.courtyard.constants.ConstantsAttribute;
import com.ocean.courtyard.core.datasource.SqlSessionImpl;
import com.ocean.courtyard.core.worldguard.WorldGuardSetting;
import com.ocean.courtyard.entity.CourtyardHome;
import com.ocean.courtyard.exception.HomeAllowFullException;
import com.ocean.courtyard.exception.HomeNotFoundException;
import com.ocean.courtyard.exception.UserExistsException;
import com.ocean.courtyard.message.Message;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

public class GrantPermissionCommand implements CommandExecutor {

    private final JavaPlugin plugin;

    private final MultiverseCore core;

    private final SqlSessionImpl sqlSession;

    private final Message message;

    private final Player player;

    private final WorldGuardSetting worldGuardSetting;

    public GrantPermissionCommand(JavaPlugin plugin, SqlSessionImpl sqlSession, Message message, Player player, WorldGuardSetting worldGuardSetting) {
        this.plugin = plugin;
        this.core = JavaPlugin.getPlugin(MultiverseCore.class);
        this.sqlSession = sqlSession;
        this.message = message;
        this.player = player;
        this.worldGuardSetting = worldGuardSetting;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        try {
            CourtyardHome home = sqlSession.getHome(player);
            //是否已经在当前家园权限列表中了
            Integer count = sqlSession.checkPlayerExists(home.getHomeId(), args[1]);
            if (count >= 1) {
                throw new UserExistsException("User " + args[1] + " already exists");
            }
            //查看家园是否已经满员了
            Integer allowCount = sqlSession.getPlayerAllowCount(home.getHomeId());
            //获取最大人数
            int maxHomePlayers = plugin.getConfig().getInt(ConstantsAttribute.MAX_HOMES_LIMIT);
            if (allowCount >= maxHomePlayers) {
                throw new HomeAllowFullException("Max allowed homes limit reached");
            }
            sqlSession.addPlayerToHome(home.getHomeId(), args[1]);
            MultiverseWorld world = core.getMVWorldManager().getMVWorld(home.getWorldName());
            worldGuardSetting.setHomeMemberFlag(world.getCBWorld(), player.getName());
            message.sendUserExistsMessage(player);
            return true;
        } catch (UserExistsException e) {
            message.sendUserExistsMessage(player);
            return false;
        } catch (HomeNotFoundException e) {
            message.sendHomeNotFoundMessage(player);
            return false;
        } catch (HomeAllowFullException e) {
            message.sendMaxHomePlayers(player);
            return false;
        }
    }
}